package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.itcast.tanhua.mytanhuadubbointerface.api.PeachApi;
import cn.itcast.tanhua.mytanhuaserver.service.PeachService;
import cn.itcast.tanhua.mytanhuaserver.service.UserInfoService;
import cn.itcast.tanhua.pojo.Sound;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.pojo.UserInfo;
import cn.itcast.tanhua.utils.UserThreadLocal;
import cn.itcast.tanhua.vo.SoundVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 张钊煌
 * @since 2021/12/6 17:07
 */
@Service
@Slf4j
public class PeachServiceImpl implements PeachService {
    @Reference(version = "1.0.0")
    private PeachApi peachApi;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public SoundVo receivePeach() throws Exception {
        User user=UserThreadLocal.get();

        //获取语音文件
        Sound sound = peachApi.receivePeach(user.getId());
        //获取该语音用户信息
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(sound.getUserId());

        //封装数据
        SoundVo soundVo=new SoundVo();
        soundVo.setId(userInfo.getUserId().intValue());
        soundVo.setAvatar(userInfo.getLogo());
        soundVo.setNickname(userInfo.getNickName());
        soundVo.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        soundVo.setAge(userInfo.getAge());
        soundVo.setSoundUrl(sound.getSoundUrl());
        String countStr = this.redisTemplate.opsForValue().get("SOUND_COUNT" + user.getId());
        Long num=Long.parseLong(countStr);
        soundVo.setRemainingTimes(num.intValue());
        return soundVo;
    }

    @Override
    public Boolean sendPeach(MultipartFile soundFile) throws Exception{
        //创建语音对象
        Sound sound=new Sound();

        User user= UserThreadLocal.get();

        //上传语音，获取语音地址
        StorePath storePath=fastFileStorageClient.uploadFile(soundFile.getInputStream(),
                soundFile.getSize(), StrUtil.subAfter(soundFile.getOriginalFilename(),".",true),
                null);

        sound.setUserId(user.getId());
        sound.setId(new ObjectId());
        sound.setSoundUrl(fdfsWebServer.getWebServerUrl()+storePath.getFullPath());
        sound.setCreated(System.currentTimeMillis());
        Boolean flag = peachApi.saveSound(sound);
        if (flag){
            //TODO 发送语音次数减一
            return true;
        }
        return false;
    }
}
